
/**
 * 获取用户ip
*/

module.exports = {
    getIp (ctx) {
        return req.headers['x-forwarded-for'] || // 判断是否有反向代理 IP
        req.connection.remoteAddress || // 判断 connection 的远程 IP
        req.socket.remoteAddress || // 判断后端的 socket 的 IP
        req.connection.socket.remoteAddress;
    },

    getSystem (ctx) {
        const useragent = ctx.header['user-agent'];
        //系统信息
        const systems = {'Windows Phone': 4, 'Windows': 1, 'Android': 2, 'iPhone': 3, 'iPad': 5};
        let systemType = 0;
        for (key in systems) {
            if(useragent.indexOf(key) >= 0){
                systemType = systems[key];
                break;
            }
        }
        return systemType;
    },

    getBrowser (ctx) {
        let browserType = 0;
        const useragent = ctx.header['user-agent'];
        //浏览器信息
        const browsers = {
            'MSIE': 1, 'Firefox': 2, 'QQBrowser': 3, 'QQ/': 3, 'UCBrowser': 4, 'MicroMessenger': 9,
            'Edge': 5, 'Chrome': 6, 'Opera': 7, 'OPR': 8, 'Safari': 9, 'Trident/': 10
        };
        for (key in browsers) {
            if(useragent.indexOf(key) >= 0){
                browserType = browsers[key];
                break;
            }
        }
        return browserType;
    },

    getCurrentDate () {
        let day = new Date();
        let year = day.getFullYear();
        let month = this.compTime(day.getMonth() + 1);
        let date = this.compTime(day.getDate());
        let hours = this.compTime(day.getHours());
        let minutes = this.compTime(day.getMinutes());
        let seconds = this.compTime(day.getSeconds());
        let result = `${year}-${(month)}-${(date)} ${(hours)}:${(minutes)}:${(seconds)}`;
        return result;
    },

    compTime (num) {
        return (num > 9 ) ? num : '0' + num;
    }
}